﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Framework.Data;
using Dal.Entities;

namespace Web.Controllers
{
    [ValidateInput(false)]
    public class NewsController : Controller
    {
        //
        // GET: /News/

        public ActionResult Index()
        {
            //var news = RepositoryFactory.GetRepository().First<News, int>(i => i.ID);
            return View();
        }

        [MyAuth(Role = 1)]
        [HttpGet]
        public ActionResult Manage()
        {
            var news = RepositoryFactory.GetRepository().First<News, int>(i => i.ID);
            return View(news);
        }

        [MyAuth(Role = 1)]
        [HttpPost]
        public ActionResult Manage(FormCollection form)
        {
            var rep = RepositoryFactory.GetRepository();
            var news = rep.First<News, int>(i => i.ID);
            if (news == null)
            {
                news = new News();
                UpdateModel(news);
                ViewBag.Result = rep.AddThenSaveChange(news) > 0;
            }
            else
            {
                rep.Attach(news);
                UpdateModel(news);
                ViewBag.Result = rep.Save() > 0;
            }
            return View(news);
        }
    }
}
